#include "rkmedia_module_config.h"

extern rkmediaInfo *rkmedia_config;
extern containerInfo *above_container_func;

rkmediaInfo::rkmediaInfo()
{
    printf("this is rkmediaInfo Init...\n");
}

rkmediaInfo::~rkmediaInfo()
{
    printf("this is rkmediaInfo destroyed...");
}

int rkmediaInfo::rkmedia_vi_init(RV1126_VI_CONFIG *vi)
{
    int ret = -1;
    VI_CHN_ATTR_S vi_attr = vi->attr;
    unsigned int vi_chn = vi->chn;
    ret = RK_MPI_VI_SetChnAttr(CAMERA_ID, vi_chn, &vi_attr);
    ret |= RK_MPI_VI_EnableChn(CAMERA_ID, vi_chn);
    if (ret)
    {
        printf("Create vi %d chn failed...\n", vi_chn);
        return -1;
    }
    else
    {
        printf("Create vi %d chn success...\n", vi_chn);
    }
    return ret;
}

int rkmediaInfo::rkmedia_venc_init(RV1126_VENC_CONFIG *venc)
{
    int ret = -1;
    VENC_CHN_ATTR_S venc_attr = venc->attr;
    unsigned int venc_chn = venc->chn;
    ret = RK_MPI_VENC_CreateChn(venc->chn, &venc_attr);
    if (ret)
    {
        printf("Create venc %d chn failed...\n", venc_chn);
        return -1;
    }
    else
    {
        printf("Create venc %d chn success...\n", venc_chn);
    }
    return ret;
}

int rkmediaInfo::rkmedia_rga_init(RV1126_RGA_CONFIG *rga)
{
    int ret = -1;
    RGA_ATTR_S rga_attr = rga->attr;
    unsigned int rga_chn = rga->chn;
    ret = RK_MPI_RGA_CreateChn(rga_chn, &rga_attr);
    if (ret)
    {
        printf("Create rga %d chn failed...\n", rga_chn);
        return -1;
    }
    else
    {
        printf("Create rga %d chn success...\n", rga_chn);
    }
    return ret;
}

int rkmediaInfo::rkmedia_module_init()
{
    int ret = -1;
    RK_MPI_SYS_Init();

    RV1126_VI_CONFIG vi;
    vi.chn = 0;
    vi.attr.pcVideoNode = VIDEO_NODE;
    vi.attr.enPixFmt = IMAGE_TYPE_NV12;
    vi.attr.enBufType = VI_CHN_BUF_TYPE_MMAP;
    vi.attr.u32BufCnt = 3;
    vi.attr.u32Height = PIX_HEIGHT_SIZE;
    vi.attr.u32Width = PIX_WIDTH_SIZE;
    vi.attr.enWorkMode = VI_WORK_MODE_NORMAL;
    ret = rkmedia_config->rkmedia_vi_init(&vi);
    if (ret)
    {
        printf("Init vi %d chn failed...\n", vi.chn);
        return -1;
    }
    else
    {
        printf("Init vi %d chn success...\n", vi.chn);
        RV1126_VI_CONTAINER vi_container;
        vi_container.index = 0;
        vi_container.vi_chn = vi.chn;
        above_container_func->set_vi_container(0, &vi_container);
    }

#if 0   
    RV1126_VI_CONFIG vi_res;
    vi_res.chn = 1;
    vi_res.attr.pcVideoNode = VIDEO_NODE;
    vi_res.attr.enPixFmt = IMAGE_TYPE_NV12;
    vi_res.attr.enBufType = VI_CHN_BUF_TYPE_MMAP;
    vi_res.attr.u32BufCnt = 3;
    vi_res.attr.u32Height = PIX_HEIGHT_SIZE;
    vi_res.attr.u32Width = PIX_WIDTH_SIZE;
    vi_res.attr.enWorkMode = VI_WORK_MODE_NORMAL;
    ret = rkmedia_config->rkmedia_vi_init(&vi_res);
    if (ret)
    {
        printf("Init vi %d chn failed...\n", vi.chn);
        return -1;
    }
    else
    {
        printf("Init vi %d chn success...\n", vi.chn);
        RV1126_VI_CONTAINER vi_container;
        vi_container.index = 1;
        vi_container.vi_chn = vi_res.chn;
        above_container_func->set_vi_container(1, &vi_container);
    }
#endif

    RV1126_RGA_CONFIG rga;
    rga.chn = 0;
    rga.attr.stImgIn.imgType = IMAGE_TYPE_NV12;
    rga.attr.stImgIn.u32Height = PIX_HEIGHT_SIZE;
    rga.attr.stImgIn.u32Width = PIX_WIDTH_SIZE;
    rga.attr.stImgIn.u32VirStride = PIX_HEIGHT_SIZE;
    rga.attr.stImgIn.u32HorStride = PIX_WIDTH_SIZE;
    rga.attr.stImgIn.u32X = 0;
    rga.attr.stImgIn.u32Y = 0;

    rga.attr.stImgOut.imgType = IMAGE_TYPE_RGB888;
    rga.attr.stImgOut.u32Height = PIX_HEIGHT_SIZE;
    rga.attr.stImgOut.u32Width = PIX_WIDTH_SIZE;
    rga.attr.stImgOut.u32VirStride = PIX_HEIGHT_SIZE;
    rga.attr.stImgOut.u32HorStride = PIX_WIDTH_SIZE;
    rga.attr.stImgOut.u32X = 0;
    rga.attr.stImgOut.u32Y = 0;

    rga.attr.u16Rotaion = 270;
    rga.attr.bEnBufPool = RK_TRUE;
    rga.attr.u16BufPoolCnt = 3;
    rga.attr.enFlip = RGA_FLIP_NULL;
    ret = rkmedia_config->rkmedia_rga_init(&rga);
    if (ret)
    {
        printf("Init rga %d chn failed...\n", rga.chn);
        return -1;
    }
    else
    {
        printf("Init rga %d chn success...\n", rga.chn);
        RV1126_RGA_CONTAINER rga_container;
        rga_container.index = 0;
        rga_container.rga_chn = rga.chn;
        above_container_func->set_rga_container(0, &rga_container);
    }

#if 0
    RV1126_RGA_CONFIG rga_res;
    rga_res.chn = 1;
    rga_res.attr.stImgIn.imgType = IMAGE_TYPE_NV12;
    rga_res.attr.stImgIn.u32Height = PIX_HEIGHT_SIZE;
    rga_res.attr.stImgIn.u32Width = PIX_WIDTH_SIZE;
    rga_res.attr.stImgIn.u32VirStride = PIX_HEIGHT_SIZE;
    rga_res.attr.stImgIn.u32HorStride = PIX_WIDTH_SIZE;
    rga_res.attr.stImgIn.u32X = 0;
    rga_res.attr.stImgIn.u32Y = 0;

    rga_res.attr.stImgOut.imgType = IMAGE_TYPE_RGB888;
    rga_res.attr.stImgOut.u32Height = PIX_HEIGHT_SIZE;
    rga_res.attr.stImgOut.u32Width = PIX_WIDTH_SIZE;
    rga_res.attr.stImgOut.u32VirStride = PIX_HEIGHT_SIZE;
    rga_res.attr.stImgOut.u32HorStride = PIX_WIDTH_SIZE;
    rga_res.attr.stImgOut.u32X = 0;
    rga_res.attr.stImgOut.u32Y = 0;

    rga_res.attr.u16Rotaion = 270;
    rga_res.attr.bEnBufPool = RK_TRUE;
    rga_res.attr.u16BufPoolCnt = 3;
    rga_res.attr.enFlip = RGA_FLIP_NULL;
    ret = rkmedia_config->rkmedia_rga_init(&rga_res);
    if (ret)
    {
        printf("Init rga_res %d chn failed...\n", rga_res.chn);
        return -1;
    }
    else
    {
        printf("Init rga_res %d chn success...\n", rga_res.chn);
        RV1126_RGA_CONTAINER rga_res_container;
        rga_res_container.index = 1;
        rga_res_container.rga_chn = rga_res.chn;
        above_container_func->set_rga_container(1, &rga_res_container);
    }
#endif

    RV1126_VENC_CONFIG venc;
    venc.chn = 0;
    venc.attr.stVencAttr.enType = RK_CODEC_TYPE_H264;
    venc.attr.stVencAttr.enRotation = VENC_ROTATION_0;
    venc.attr.stVencAttr.imageType = IMAGE_TYPE_BGR888;
    venc.attr.stVencAttr.u32PicHeight = PIX_HEIGHT_SIZE;
    venc.attr.stVencAttr.u32PicWidth = PIX_WIDTH_SIZE;
    venc.attr.stVencAttr.u32Profile = 66;
    venc.attr.stVencAttr.u32VirHeight = PIX_HEIGHT_SIZE;
    venc.attr.stVencAttr.u32VirWidth = PIX_WIDTH_SIZE;

    venc.attr.stRcAttr.enRcMode = VENC_RC_MODE_H264CBR;
    venc.attr.stRcAttr.stH264Cbr.u32Gop = RK_FPS;
    venc.attr.stRcAttr.stH264Cbr.u32BitRate = PIX_HEIGHT_SIZE * PIX_WIDTH_SIZE * 3;
    venc.attr.stRcAttr.stH264Cbr.u32SrcFrameRateDen = 1;
    venc.attr.stRcAttr.stH264Cbr.u32SrcFrameRateNum = RK_FPS;
    venc.attr.stRcAttr.stH264Cbr.fr32DstFrameRateDen = 1;
    venc.attr.stRcAttr.stH264Cbr.fr32DstFrameRateNum = RK_FPS;
    ret = rkmedia_config->rkmedia_venc_init(&venc);
    if (ret)
    {
        printf("Init venc %d chn failed...\n", venc.chn);
        return -1;
    }
    else
    {
        printf("Init venc %d chn success...\n", venc.chn);
        RV1126_VENC_CONTAINER venc_container;
        venc_container.index = 0;
        venc_container.venc_chn = venc.chn;
        above_container_func->set_venc_container(0, &venc_container);
    }

#if 1
    VO_CHN_ATTR_S stVoChn;
    memset(&stVoChn, 0, sizeof(VO_CHN_ATTR_S));
    stVoChn.pcDevNode = VO_DEV_NODE;
    stVoChn.emPlaneType = VO_PLANE_OVERLAY;
    stVoChn.enImgType = IMAGE_TYPE_RGB888;
    stVoChn.u16Zpos = 0;
    stVoChn.stImgRect.s32X = 0;
    stVoChn.stImgRect.s32Y = 0;
    stVoChn.stImgRect.u32Height = PIX_HEIGHT_SIZE;
    stVoChn.stImgRect.u32Width = PIX_WIDTH_SIZE;
    stVoChn.stDispRect.s32X = 0;
    stVoChn.stDispRect.s32Y = 0;
    stVoChn.stDispRect.u32Height = PIX_HEIGHT_SIZE;
    stVoChn.stDispRect.u32Width = PIX_WIDTH_SIZE;
    ret = RK_MPI_VO_CreateChn(0, &stVoChn);
    if (ret)
    {
        printf("ERROR: create VO[0:0] failed! ret=%d\n", ret);
        return -1;
    }
#endif

    cout << endl
         << "###############################################" << endl;
    cout << "--------------Rkmedia config area--------------" << endl
         << endl;
    cout << "RKAIQ-FilePath:" << IQ_FILE_PATH << endl;
    cout << "VideoNode:" << VIDEO_NODE << endl;
    cout << "inPixelSize(width, height):" << PIX_WIDTH_SIZE << "x" << PIX_HEIGHT_SIZE << endl
         << endl;
    cout << "VO-DevNode:" << VO_DEV_NODE << endl;
    cout << "outPixelSize(width, height):" << PIX_WIDTH_SIZE << "x" << PIX_HEIGHT_SIZE << endl;
    cout << "Fps:" << RK_FPS << endl;
    cout << "###############################################" << endl
         << endl;

    return 0;
}